package com.anysoft.web;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.WebView;

import com.anysoft.HostConstant;
import com.anysoft.R;
import com.anysoft.WebViewNetworkProtocol;
import com.anysoft.event.FundBuyResultEvent;
import com.anysoft.event.TimeOutEvent;
import com.anysoft.me.base.BaseWebViewActivity;
import com.anysoft.utils.ActivityUtils;
import com.anysoft.utils.CommonUtil;
import com.anysoft.utils.MentorLogger;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import butterknife.OnClick;

import static com.anysoft.HostConstant.HOST_WEB_URL_HEADER;

/**
 * Created by wwq on 2017/3/16.
 * 预约赎回
 */

public class AssetsFundGoCashActivity extends BaseWebViewActivity {

    private String param;
    private String mainTitle;
    private boolean isTheLastPage = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isShowTitleBar(true);
        param = getIntent().getStringExtra("param");
        mainTitle = getIntent().getStringExtra("mainTitle");
        loadUrl = HOST_WEB_URL_HEADER + "fundCash.html?" + param;

        title.setVisibility(View.GONE);
        cmtvTitle.setText(mainTitle);
        cmtvTitle.setVisibility(View.VISIBLE);
        if (CommonUtil.isNetWorkConnected(this)) {
            setWebViewConfig(loadUrl);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onshouldOverrideUrlLoadingCallBack(WebView webview, String url) {
        loadUrl = url;
        MentorLogger.Log(TAG, "url=" + url);

        String url_decode = null;
        try {
            url_decode = URLDecoder.decode(url, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        MentorLogger.Log(TAG, "url_decode=" + url_decode);
        if (url.contains("prompt.html")) {
            mIntent.setClass(mActivity, SelectWarmPromptActivity.class);
            mIntent.putExtra("url", url);
            startActivity(mIntent);
        } else if (url.contains("cashResult")) {
            String fundcode;
            Map<String, String> map = new HashMap<>();
            String[] params = url_decode.split("\\?")[1].split("&");
            for (int i = 0; i < params.length; i++) {
                String key = params[i].split("=")[0];
                String value = "";
                try {
                    value = params[i].split("=")[1];
                } catch (Exception exception) {
                    MentorLogger.Log(TAG, "value为null");
                }
                map.put(key, value);
            }
            fundcode = map.get("fundCode");

            isTheLastPage = true;
            leftButton.setVisibility(View.GONE);
            ivLeftButton.setVisibility(View.GONE);
//            if ("482002".equals(fundcode) || "000677".equals(fundcode)) {
//                cmtvTitle.setText("取现结果");
//            } else {
            cmtvTitle.setText("赎回结果");
//            }
            webview.loadUrl(url);
        } else if (url.contains("transResult")) {
//            mIntent.setClass(mActivity, AssetsFundtransResultActivity.class);
//            String param = url.split("\\?")[1];
//            mIntent.putExtra("param", param);
//            mIntent.putExtra("title", "赎回结果");
//            startActivity(mIntent);
            isTheLastPage = true;
            leftButton.setVisibility(View.GONE);
            ivLeftButton.setVisibility(View.GONE);
            cmtvTitle.setText("转换结果");
            webview.loadUrl(url);
        } else if (url.contains(WebViewNetworkProtocol.GOTO_FUND_LIST)) {
            EventBus.getDefault().post(new FundBuyResultEvent(true));
            finish();
        } else if (url.contains(WebViewNetworkProtocol.GOTO_LOGIN)) {
            ActivityUtils.gotoLogin(mActivity, hasUser(), false, findUserId());
            EventBus.getDefault().post(new TimeOutEvent(true));
        } else if (url.contains(WebViewNetworkProtocol.GO_QUESTION)) {
            startRiskActivity(null);
        } else if (url.contains("ZXHBJJKSSH.html")) {
            mIntent.setClass(mActivity, AgreementFromWebActivity.class);
            mIntent.putExtra("html", url);
            startActivity(mIntent);
        } else if (url.contains(WebViewNetworkProtocol.GO_IDCARD_UPLOAD)) {
            ActivityUtils.gotoUploadIditityCardActivity(this, "0", "2");
        } else {
            isTheLastPage = false;
            leftButton.setVisibility(View.VISIBLE);
            ivLeftButton.setVisibility(View.VISIBLE);
            cmtvTitle.setText("赎回");
            webview.loadUrl(url);
        }
    }

    @OnClick({R.id.leftButton, R.id.leftButton2})
    public void onClick() {
        finish();
    }

    @Override
    public void onBackPressed() {
        if (isTheLastPage) {
            EventBus.getDefault().post(new FundBuyResultEvent(true));
        }
        super.onBackPressed();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGestureSetCallBack(FundBuyResultEvent event) {
        if (event.isSuccess) {
            finish();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTimeOutCallBack(TimeOutEvent event) {
        if (event.isSuccess) {
            finish();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == HostConstant.STRAT_UPLOAD_ID) {
            generateUrl("fundCash", param);
            if (CommonUtil.isNetWorkConnected(mActivity)) {
                reLoadPage(loadUrl);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
